package main;

import java.util.List;

import javax.swing.JOptionPane;

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.matchers.KeyMatcher;
import org.quartz.TriggerKey;
import pojo.Schedule;

public class SetupScheduler {
	//private JobDetail job;
	//private Scheduler sched;
	//private Trigger trigger;
	public void setupOrUpdate(Schedule s){
		try {
			Scheduler sched = getDefaultScheduler();
			//List<JobExecutionContext> temp1 = null;
			//try{
			//	temp1 = sched.getCurrentlyExecutingJobs();
			//}catch(SchedulerException se){
			//	se.printStackTrace();
			//}
			
			//if(temp1.isEmpty()){
			CronScheduleBuilderFactory c = new CronScheduleBuilderFactory();
			List<String> temp = sched.getJobGroupNames();
			if(temp.isEmpty()){
				if(s.getManualCollect().compareTo("false") == 0){
						//chua co job va schedule dc thiet lap tu dong
					JobDetail threadPoolJob = JobBuilder.newJob(ThreadPoolJob.class).withIdentity("ThreadPoolJob", "group1").build();
					Trigger trigger = TriggerBuilder.newTrigger().withIdentity("ThreadPoolTrigger", "group1").withSchedule(c.returnNewCronScheduleBuilder(s)).forJob("ThreadPoolJob", "group1").build();
						
						//System.setProperty("org.quartz.scheduler.makeSchedulerThreadDaemon", "false"); 
					sched.scheduleJob(threadPoolJob, trigger);
						//System.out.println(sched.getTriggerGroupNames());
						//sched.getListenerManager().addJobListener(new ThreadPoolListener(), KeyMatcher.keyEquals(JobKey.jobKey("ThreadPoolJob", "group1")));
						
					sched.start();
				}
			}else{	
					//reschedule job
				if(s.getManualCollect().compareTo("true") == 0){
						//pause trigger
					sched.pauseTrigger(TriggerKey.triggerKey("ThreadPoolTrigger", "group1"));
				}else{
						//replace trigger
					Trigger oldTrigger = sched.getTrigger(TriggerKey.triggerKey("ThreadPoolTrigger", "group1"));
					TriggerBuilder tb = oldTrigger.getTriggerBuilder();
					Trigger newTrigger = tb.withSchedule(c.returnNewCronScheduleBuilder(s)).build();
					sched.rescheduleJob(oldTrigger.getKey(), newTrigger);
					sched.start();
				}
			}
			//}else{
			//	JOptionPane.showMessageDialog(null, "Schedule dang chay!");
			//}
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void startNow(){
		try {
			Scheduler sched = getDefaultScheduler();
			sched.triggerJob(JobKey.jobKey("ThreadPoolJob", "group1"));
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void cancelNow(){
		try {
			Scheduler sched = getDefaultScheduler();
			sched.interrupt(JobKey.jobKey("ThreadPoolJob", "group1"));
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public Scheduler getDefaultScheduler(){
		Scheduler temp = null;
		try {
			temp = StdSchedulerFactory.getDefaultScheduler();
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return temp;
	}
}
